package com.c2b.algorithm.leetcode.base;

import java.util.ArrayList;
import java.util.List;

/**
 * <a href="https://leetcode.cn/problems/summary-ranges/description/?envType=study-plan-v2&envId=top-interview-150">汇总区间(Summary Ranges)</a>
 * <p>给定一个  无重复元素 的 有序 整数数组 nums 。</p>
 * <p>返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表 。也就是说，nums 的每个元素都恰好被某个区间范围所覆盖，并且不存在属于某个范围但不属于 nums 的数字 x 。</p>
 * <p>
 * 列表中的每个区间范围 [a,b] 应该按如下格式输出：
 *     <ul>
 *         <li>"a->b" ，如果 a != b</li>
 *         <li>"a" ，如果 a == b</li>
 *     </ul>
 * </p>
 * <p>
 *     <b>示例：</b>
 *     <pre>
 * 示例 1：
 *      输入：nums = [0,1,2,4,5,7]
 *      输出：["0->2","4->5","7"]
 *      解释：区间范围是：
 *          [0,2] --> "0->2"
 *          [4,5] --> "4->5"
 *          [7,7] --> "7"
 *
 * 示例 2：
 *      输入：nums = [0,2,3,4,6,8,9]
 *      输出：["0","2->4","6","8->9"]
 *      解释：区间范围是：
 *          [0,0] --> "0"
 *          [2,4] --> "2->4"
 *          [6,6] --> "6"
 *          [8,9] --> "8->9"
 *     </pre>
 * </p>
 * <p>
 *     <b>提示：</b>
 *     <ul>
 *         <li>0 <= nums.length <= 20</li>
 *         <li>-2^31 <= nums[i] <= 2^31 - 1</li>
 *         <li>nums 中的所有值都 互不相同</li>
 *         <li>nums 按升序排列</li>
 *     </ul>
 * </p>
 *
 * @author c2b
 * @since 2023/10/16 18:06
 */
public class LC0228SummaryRanges_S {
    static class Solution {
        public List<String> summaryRanges(int[] nums) {
            List<String> retList = new ArrayList<>();
            int length = nums.length;
            if (length == 0) {
                return retList;
            }
            int currIndex = 0;
            while (currIndex < length) {
                int nextIndex = currIndex;
                // 到达倒数第一个位置即结束，避免越界
                while (nextIndex < length - 1 && nums[nextIndex] + 1 == nums[nextIndex + 1]) {
                    ++nextIndex;
                }
                StringBuilder temp = new StringBuilder(Integer.toString(nums[currIndex]));
                if (currIndex != nextIndex) {
                    temp.append("->").append(nums[nextIndex]);
                }
                retList.add(temp.toString());
                currIndex = nextIndex + 1;
            }
            return retList;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        for (String range : solution.summaryRanges(new int[]{0, 1, 2, 4, 5, 7})) {
            System.out.print(range + "  ");
        }
        System.out.println();
        for (String range : solution.summaryRanges(new int[]{0, 2, 3, 4, 6, 8, 9})) {
            System.out.print(range + "  ");
        }
        System.out.println();
        for (String range : solution.summaryRanges(new int[]{0})) {
            System.out.print(range + "  ");
        }
        System.out.println();
        for (String range : solution.summaryRanges(new int[]{})) {
            System.out.print(range + "  ");
        }
        System.out.println();
        for (String range : solution.summaryRanges(new int[]{1, 2, 3})) {
            System.out.print(range + "  ");
        }
        System.out.println();
    }
}
